package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kaoM1Jy
 * @date 2025/10/8 19:59
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result getAll() {
        log.info("查询所有角色");
        List<Role> list= roleService.getAll();
        return Result.success(list);
    }

    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色：{}", role);
        roleService.update(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("查询角色：{}", id);
        Role role = roleService.getByID(id);
        return Result.success(role);
    }


    @PostMapping
    public Result addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result deleteByID(@PathVariable Integer id) {
        log.info("删除角色：{}", id);
        roleService.deleteByID(id);
        return Result.success();
    }

    @GetMapping
    public Result page (
        @RequestParam(name = "name", required = false) String name,
        @RequestParam(name = "label", required = false) String label,
        @RequestParam(name = "page", defaultValue = "1") Integer page,
        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        log.info("分页查询角色：{}", name);
        PageResult<Role> pageResult = roleService.page(name, label, page, pageSize);
        return Result.success(pageResult);
    }
}
